import Vue from "vue";
import Router from "vue-router";
import Main from "../layouts/main.vue";
import CommonViews from "libs-common-views";
import {
    getToken
} from "../utils/login";
import store from "../store/index";

Vue.use(Router);
const router = new Router({
    mode: "history",
    base: process.env.NODE_ENV === "production" ? "/portal/" : "/",
    routes: [{
            path: "/",
            name: "login",
            component: CommonViews.LibsLogin,
        },
        {
            path: "/main",
            name: "main",
            component: Main,
            children: [{
                    path: "",
                    redirect: "home",
                },
                {
                    path: "home",
                    name: "home",
                    component: () => import("../views/Home.vue"),
                },
                { // 质检任务
                    path: "qc-task",
                    name: "qc-task",
                    component: () => import("../views/qc/Task"),
                },
                { // 质检任务详情
                    path: "taskDetail",
                    name: "taskDetail",
                    component: () => import("../views/qc/TaskDetail"),
                },
                { // 检验项目
                    path: "qc-item",
                    name: "qc-item",
                    component: () => import("../views/qc/Project"),
                },
                { // 检验标准
                    path: "qc-standard",
                    name: "qc-standard",
                    component: () => import("../views/qc/Standard"),
                },
                { // 检验标准详情
                    path: "standardDetail",
                    name: "standardDetail",
                    component: () => import("../views/qc/StandardDetail"),
                },
                { // 样品管理
                    path: "quality/sample-library",
                    name: "quality/sample-library",
                    component: () => import("../views/qc/Sample"),
                },
            ],
        },
    ],
});

//to为向后走的路由对象，包括路由的完整信息
//from为从哪跳来的路由对象
//next()控制路由向下走，重新定义路由跳转的路由next(‘路由路径)

/** 处理跳转业务 **/
router.beforeEach((to, from, next) => {
    const token = getToken();
    if (token) {
        if (to.meta.isAddedActions == undefined || to.meta.isAddedActions == false) {
            to.meta.isAddedActions = true;
            store
                .dispatch("oauth/findActionOfModule", to.path)
                .then(res => {
                    if (res.data.length > 0) {
                        to.meta.actions = res.data.map(item => item.name);
                    }
                    next();
                })
                .catch(() => {
                    router.replace("/");
                });
        } else {
            next();
        }
    } else {
        if (to.name == "login") {
            next();
        } else {
            next({
                name: "login"
            });
        }
    }
});

export default router;